package com.eco.fanliapp.admin.annotation;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.dao.SysLogMapper;
import com.eco.fanliapp.entity.SysLog;
import com.eco.fanliapp.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Aspect
@Component
public class SysLogAspect extends BaseController {

     private static final Logger log = LoggerFactory.getLogger(SysLogAspect.class);

    @Autowired
    private SysLogMapper sysLogMapper;

    @AfterReturning(value = "@annotation(com.eco.fanliapp.admin.annotation.SysLogAnnotation)", returning = "response")
    public void doAfterReturning(JoinPoint joinPoint, Object response) {
        addSysLog(joinPoint, 1, response);
    }

    @AfterReturning(value = "@annotation(com.eco.fanliapp.admin.annotation.SysLogAnnotation)", returning = "response")
    public void doAfterThrowing(JoinPoint joinPoint, Object response) {
        addSysLog(joinPoint, 2, response);
    }

    private void addSysLog(JoinPoint joinPoint, int type, Object response) {
        try {
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            SysLogAnnotation annotation = methodSignature.getMethod().getAnnotation(SysLogAnnotation.class);
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            SysLog sysLog = new SysLog();
            UserVO authentication = getAuthentication();
            sysLog.setUserId(authentication == null ? 0 : getAuthentication().getUserId());//用户id
            sysLog.setMethodExplain(annotation.value());//接口说明
            sysLog.setCreatTime(new Date());//创建日期
            sysLog.setMethod(request.getMethod());//请求方法类型
            sysLog.setRequestUrl(request.getRequestURL().toString());//请求的url
            sysLog.setUserType(annotation.userType());//用户类型
            sysLog.setRequestParams(request.getQueryString() == null ? StringUtils.strip(JSON.toJSON(joinPoint.getArgs()).toString(), "[]") : request.getQueryString());//请求参数
            if(sysLog.getRequestParams() == null)sysLog.setRequestParams(JSONObject.toJSONString(request.getParameterMap()));
            if (type == 1) sysLog.setResponseParams(JSONObject.toJSONString(response));//响应参数
            else if (type == 2) {
                JSONObject json = (JSONObject) JSON.toJSON(response);
                sysLog.setResponseParams(json.getString("message"));//响应参数
            }
            int i = sysLogMapper.insertSelective(sysLog);
            log.info("日志是否保存成功：" + i);
        } catch (Exception e) {
            log.info("日志添加失败" + e.getMessage());
        }

    }
}
